package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.cust.UserInfoConstants;
import com.gopay.common.domain.UserInfo;
import com.gopay.common.user.dao.UserInfoDAO;

@Repository("commonUserInfoDAO")
public class UserInfoDAOImpl extends BaseDAOImpl<UserInfo, Serializable> implements UserInfoDAO {

	protected Logger logger = LoggerFactory.getLogger(UserInfoDAOImpl.class);
	
	@Override
	public List<UserInfo> queryUsersByCondition(Map<String, Object> params) {
		String hql = "from UserInfo t where 1=1";
		for( Map.Entry<String, Object> entry : params.entrySet() ){
			hql += " AND "+entry.getKey()+"=:"+entry.getKey();
		}
		return this.findByHql(hql, params);
	}

	@Override
	public List<UserInfo> queryUsersByCreateTs(String startTime, String endTime) {
		String hql = "from UserInfo t where 1=1 AND t.createdTs >= to_timestamp('"+startTime+"' , 'yyyy-MM-dd HH24:MI:ss.ff') AND t.createdTs <= to_timestamp('"+endTime+"' , 'yyyy-MM-dd HH24:MI:ss.ff') order by t.createdTs asc";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userType", UserInfoConstants.USER_TYPE_PERSON);
		params.put("userStat", UserInfoConstants.USER_STAT_NORMAL);
		params.put("userResource", "WALLET_APP");
		for( Map.Entry<String, Object> entry : params.entrySet() ){
			hql += " AND t."+entry.getKey()+"=:"+entry.getKey();
		}
		return this.findByHql(hql, params);
	}

	@Override
    public List<UserInfo> getByCustId(String custId) {
        String hql = "from UserInfo where custId=:custId and userStat!=:userStat ";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custId", custId);
        paramMap.put("userStat", UserInfoConstants.USER_STAT_CANCEL);
        List<UserInfo> list = this.findByHql(hql, paramMap);
        return list.isEmpty() ? null : list;
    }
	/**
	 * add by tangyang at 20171020
	 */
	@Override
    public UserInfo getByCustIdAndOperId(String custId,String operId) {
        String hql = "from UserInfo where custId=:custId and operId=:operId ";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custId", custId);
        paramMap.put("operId", operId);
        List<UserInfo> list = this.findByHql(hql, paramMap);
        return list.isEmpty() ? null : list.get(0);
    }
}
